package idv.kentYeh.util;

import java.util.ArrayList;
import java.util.List;

/**
 * 物件供應器，物件總量太大時，分段取出物件
 * @author Kent Yeh
 */
public abstract class AbstractSupplier<T> implements Supplier<T> {

    private int size = 50;
    private T prev = null;

    public AbstractSupplier() {
    }

    /**
     * @param size  每次取出物件數量
    
     */
    public AbstractSupplier(int size) {
        this.size = size;
    }

    /**
     * 每次取出物件數量
     * @return 物件數量
     */
    public int getSize() {
        return size;
    }

    /**
     * 設定每次取出物件數量
     * @param size 物件數量 
     */
    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public T previous() {
        return prev;
    }

    @Override
    public List request() {
        return request(size);
    }

    @Override
    public List<T> request(int size) {
        List<T> result = new ArrayList<T>();
        int i = 0;
        while (i < size) {
            T t = next();
            if (t == null) {
                break;
            } else {
                prev = t;
                result.add(t);
            }
        }
        return result;
    }

    @Override
    public void reset() {
        prev = null;
    }
}
